<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../base.jsp"%>
<html>
<head>
    <meta charset="utf-8">
    <title>学生选择列表页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="css/public.css" media="all">
    <style>
        body{
            margin: 10px 0 0 0 !important;
        }

    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="layui-elem-field site-demo-button" style="margin-top: 30px;padding: 10px">
            <legend>${className}</legend>
            <div>
                <c:forEach items="${list}" var="user">
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-primary realName-btn-delete" title="点击删除" id="${user.id}">${user.number}-${user.realName}</button>
                </c:forEach>
            </div>
        </fieldset>
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">关键字</label>
                            <div class="layui-input-inline">
                                <input type="text" name="searchKey" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn" ><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 选择 </button>

            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>



    </div>
</div>
<script src="lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            element = layui.element;

        table.render({
            elem: '#currentTableId',
            url: '${basePath}/studentDataJson.cl?roleId=3&classId=${classId}',
            toolbar: '#toolbarDemo',
            defaultToolbar: [],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'number', title: '学号', sort: true},
                {field: 'realName', title: '姓名'},
                {field: 'account', title: '用户名'},
                {field: 'idNumber', title: '身份证号',templet:function (d){
                    var idNumber = d.idNumber;
                    if(idNumber!=null && idNumber!=''){
                        return idNumber.substring(0,4)+"......"+idNumber.substring(idNumber.length-4);
                    }else{
                        return '';
                    }
                    }},
                {field: 'gender', title: '性别',sort: true,templet: function(d){if(d.gender == 1){return '男'}else if(d.gender == 0){return '女'}else{return ''}} },
                {field: 'phone', title: '手机号码'},
                {field: 'roleName', title: '角色'},
                {field: 'email', title: '电子邮件'},
            ]],
            limits: [2, 5, 10, 15, 20],
            limit: 5,
            page: true,
            skin: 'line',
            parseData: function(res){ //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
                var result;
                if(this.page.curr){
                    result = res.data.slice(this.limit*(this.page.curr-1),this.limit*this.page.curr);
                }
                else{
                    result=res.data.slice(0,this.limit);
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": result //解析数据列表
                };
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchKey: data.field.searchKey
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听提交操作
                var checkStatus = table.checkStatus('currentTableId');
                var data = checkStatus.data;
                //拼接ID
                var ids = '';//1,2,3,4
                for(var i=0;i<data.length;i++){
                    if(ids==''){
                        ids = data[i].id;
                    }else{
                        ids += ","+data[i].id;
                    }
                }
                //判断是否选择了删除行
                if(ids==''){
                    layer.alert('请选择要提交的行');
                }else{
                    var classId = '${classId}';
                    $.ajax({
                        url:'${basePath}/saveStudentSelect.cl',
                        type:'get',
                        data:{'ids':ids,'classId':classId},
                        dataType: "json",
                        success:function (res){
                            if(res.code===0){
                                layer.msg(res.msg,function(){
                                    //重新加载页面
                                    window.location.reload();
                                })
                            }else{
                                layer.alert(res.msg);
                            }
                        }
                    })
                }

            }
        });



    });
</script>

</body>
</html>